package com.zzb.valid.controller;

import com.zzb.valid.dto.ResultEntity;
import com.zzb.valid.dto.ValidDTO;
import com.zzb.valid.dto.ValidatedDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author create by zhouzongbo on 2020/10/27.
 */
@Valid
@Slf4j
@RestController
@RequestMapping("/valid")
public class ValidController {

    @PostMapping("/1")
    public ResultEntity<Boolean> valid(@RequestBody @Valid ValidDTO valid) {
        log.info("校验ValidDTO参数{}.", valid.toString());
        return ResultEntity.success(Boolean.TRUE);
    }

    /**
     * 多层次校验
     * 一个类中局部变量是类时, 需要在类上添加@Valid注解.
     * @param valid valid
     * @return ResultEntity
     */
    @PostMapping("/2")
    public ResultEntity<Boolean> validated(@RequestBody @Validated ValidatedDTO valid) {
        log.info("校验多层次校验参数{}.", valid.toString());
        return ResultEntity.success(Boolean.TRUE);
    }
}
